<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="专家姓名" prop="expName">
        <el-input
          v-model="queryParams.expName"
          placeholder="请输入专家姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专家单位" prop="expAffi">
        <el-input
          v-model="queryParams.expAffi"
          placeholder="请输入专家单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="utype">
        <el-select v-model="queryParams.utype" placeholder="请选择类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.search_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['intelligence:expert:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['intelligence:expert:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['intelligence:expert:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['intelligence:expert:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="expertList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="60">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}
        </template>
      </el-table-column>
      <el-table-column label="专家姓名" align="center" prop="expName" />
      <el-table-column label="专家单位" align="center" prop="expAffi" />
      <el-table-column label="开始日期" align="center" prop="startTime" />
      <el-table-column label="结束日期" align="center" prop="endTime" />
      <el-table-column label="创建日期" align="center" prop="creatTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creatTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数据库" align="center" prop="dbase" />
      <el-table-column label="更新日期" align="center" prop="lastUpdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastUpdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="utype">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.search_type" :value="scope.row.utype"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['intelligence:expert:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh-right"
            @click="handleSearch(scope.row)"
            v-hasPermi="['intelligence:expert:edit']"
          >检索</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['intelligence:expert:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改专家对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="类型" prop="utype">
          <el-select v-model="form.utype" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.search_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专家姓名" prop="expName">
          <el-input v-model="form.expName" placeholder="请输入专家姓名" @blur="getPinYin"/>
        </el-form-item>
        <el-form-item label="专家单位" prop="expAffi" :inline="true">
          <el-input v-model="form.expAffi" placeholder="请输入专家单位" style="width:400px" />
          <el-button @click="choseAff" type="primary">选择</el-button>
        </el-form-item>
        <el-form-item label="姓名拼音" prop="pinYin">
          <el-input v-model="form.pinYin" placeholder="请输入姓名拼音" />
        </el-form-item>
        <el-form-item label="拼音列表" prop="pinYinList">
          <el-input v-model="form.pinYinList" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="中文检索单位" prop="expSeachAffCn">
          <el-input v-model="form.expSeachAffCn" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="英文检索单位" prop="expSeachAffEn">
          <el-input v-model="form.expSeachAffEn" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="数据库">
          <el-checkbox-group v-model="form.dbase">
            <el-checkbox
              v-for="dict in dict.type.biz_databases"
              :key="dict.value"
              :label="dict.label">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="检索时间" :inline="true">
          <el-select
            v-model="form.startTime"
            placeholder="请选择"
            filterable
            style="height: 32px; line-height: 32px; width: 150px"
          >
            <el-option
              v-for="item in years"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          -
          <el-select
            v-model="form.endTime"
            placeholder="请选择"
            filterable
            style="height: 32px; line-height: 32px; width: 150px"
          >
            <el-option
              v-for="item in years"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="affTitle" :visible.sync="affOpen" width="600px" append-to-body>
      <el-form :model="queryAffParams" ref="queryAffForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="机构名称" prop="title">
          <el-input
            v-model="queryAffParams.title"
            placeholder="请输入机构名称"
            clearable
            size="small"
            @keyup.enter.native="handleAffQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleAffQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetAffQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="affLoading" :data="affList" @selection-change="handleSelectionChange">
        <el-table-column label="序号" width="60">
          <template slot-scope="scope">
            {{ (queryAffParams.pageNum - 1) * queryAffParams.pageSize + scope.$index + 1}}
          </template>
        </el-table-column>
        <el-table-column label="机构名称" align="left" prop="title" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleSelection(scope.row)"
              v-hasPermi="['intelligence:expert:edit']"
            >选择</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="affTotal>0"
        :total="affTotal"
        :page.sync="queryAffParams.pageNum"
        :limit.sync="queryAffParams.pageSize"
        @pagination="getAffList"
      />
    </el-dialog>
  </div>
</template>

<script>
import { pinyin } from 'pinyin-pro';
import { listExpert, getExpert, delExpert, addExpert, updateExpert, exportExpert, handleSearch  } from "@/api/intelligence/expert";
import { listBibAffi, getBibAffi, delBibAffi, addBibAffi, updateBibAffi, exportBibAffi } from "@/api/intelligence/bibAffi";

export default {
  name: "Expert",
  dicts: ['search_type', 'biz_databases'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专家表格数据
      expertList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        expName: null,
        expAffi: null,
        utype: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        expName: [
          { required: true, message: "专家姓名不能为空", trigger: "blur" }
        ],
        expAffi: [
          { required: true, message: "专家单位不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开始日期不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "结束日期不能为空", trigger: "blur" }
        ],
        creatTime: [
          { required: true, message: "创建日期不能为空", trigger: "blur" }
        ],
      },
      affTitle: "",
      affOpen: false,
      affList: [],
      // 查询参数
      queryAffParams: {
        pageNum: 1,
        pageSize: 10,
        title: null
      },
      affLoading: true,
      affTotal: 0,
      years: []


    };
  },
  created() {
    this.getList();
  },
  methods: {
    getAffList(){
      this.affLoading = true;
      listBibAffi(this.queryAffParams).then(response => {
        this.affList = response.rows;
        this.affTotal = response.total;
        this.affLoading = false;
      });
    },
    /** 查询专家列表 */
    getList() {
      this.loading = true;
      listExpert(this.queryParams).then(response => {
        this.expertList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        expId: null,
        expName: null,
        expAffi: null,
        pinYin: null,
        pinYinList: null,
        expSeachAffCn: null,
        expSeachAffEn: null,
        startTime: null,
        endTime: null,
        creatTime: null,
        dbase: [],
        lastUpdate: null,
        utype: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleAffQuery(){
      this.queryAffParams.pageNum = 1;
      this.getAffList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 重置按钮操作 */
    resetAffQuery() {
      this.resetForm("queryAffForm");
      this.handleAffQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.expId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加专家";
      let myDate = new Date();
      let year = myDate.getFullYear();
      this.initSelectYear(year);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const expId = row.expId || this.ids
      getExpert(expId).then(response => {
        this.form = response.data;
        this.form.dbase = this.form.dbase.split(",");
        this.open = true;
        this.title = "修改专家";
        let myDate = new Date();
        let year = myDate.getFullYear();
        this.initSelectYear(year);
      });
    },
    handleSearch(row){
      this.reset();
      handleSearch(row).then(response => {
        this.$modal.msgSuccess("检索成功");
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.dbase = this.form.dbase.join(",");
          if (this.form.expId != null) {
            updateExpert(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExpert(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const expIds = row.expId || this.ids;
      this.$modal.confirm('是否确认删除专家编号为"' + expIds + '"的数据项？').then(function() {
        return delExpert(expIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有专家数据项？').then(() => {
        this.exportLoading = true;
        return exportExpert(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    /** 新增按钮操作 */
    choseAff() {
      this.affOpen = true;
      this.affTitle = "选择机构";
      this.affLoading = true;
      listBibAffi(this.queryAffParams).then(response => {
        this.affList = response.rows;
        this.total = response.total;
        this.affLoading = false;
      });
    },
    handleSelection(row){
      this.affOpen = false;
      this.form.expAffi = row.title;
      this.form.expSeachAffCn = row.csTitle;
      this.form.expSeachAffEn = row.esTitle;
    },
    getPinYin(){
      //console.log(this.form.expName)
      //console.log(pinyin(this.form.expName, { toneType: 'none' }))
      this.form.pinYin = pinyin(this.form.expName, { toneType: 'none' });
      let pinyinArr = pinyin(this.form.expName, { toneType: 'none', type: 'array' });
      switch (pinyinArr.length) {
        case 2:
          this.form.pinYinList = pinyinArr[0]+" "+pinyinArr[1]+";" +pinyinArr[1]+" "+pinyinArr[0];
          break;
        case 3:
          this.form.pinYinList = pinyinArr[0]+" "+pinyinArr[1]+" "+pinyinArr[2]+";"
            +pinyinArr[0]+" "+pinyinArr[1]+pinyinArr[2]+";"
            +pinyinArr[1]+pinyinArr[2]+" "+pinyinArr[0]+";"
            +pinyinArr[0]+", "+pinyinArr[1]+pinyinArr[2]+";"
            +pinyinArr[1]+pinyinArr[2]+", "+pinyinArr[0]+";"
            +pinyinArr[0]+", "+pinyinArr[1]+"-"+pinyinArr[2]+";"
            +pinyinArr[1]+"-"+pinyinArr[2]+", "+pinyinArr[0]+";"
            +pinyinArr[0]+", "+getFristChartClean(pinyinArr[1])+getFristChartClean(pinyinArr[2])+";"
            +pinyinArr[0]+" "+getFristChartClean(pinyinArr[1])+getFristChartClean(pinyinArr[2]);
          break;
        case 4:
          this.form.pinYinList = pinyinArr[0]+pinyinArr[1]+" "+pinyinArr[2]+pinyinArr[3]+";"
            +pinyinArr[2]+pinyinArr[3]+" "+pinyinArr[0]+pinyinArr[1]+";"
            +pinyinArr[0]+pinyinArr[1]+", "+pinyinArr[2]+pinyinArr[3]+";"
            +pinyinArr[2]+pinyinArr[3]+", "+pinyinArr[0]+pinyinArr[1]+";"
            +pinyinArr[0]+pinyinArr[1]+", "+pinyinArr[2]+"-"+pinyinArr[3]+";"
            +pinyinArr[2]+"-"+pinyinArr[3]+", "+pinyinArr[0]+pinyinArr[1]+";"
            +pinyinArr[0]+pinyinArr[1]+", "+getFristChartClean(pinyinArr[2])+getFristChartClean(pinyinArr[3])+";"
            +pinyinArr[0]+pinyinArr[1]+" "+getFristChartClean(pinyinArr[2])+getFristChartClean(pinyinArr[3])+";"
            +pinyinArr[0]+pinyinArr[1]+" "+pinyinArr[2]+"-"+pinyinArr[3]+";"
            +pinyinArr[2]+"-"+pinyinArr[3]+" "+pinyinArr[0]+pinyinArr[1];
          break;
        default:
          this.form.pinYinList = pinyin(this.form.expName, { toneType: 'none' });
      }
    },
    initSelectYear(year) {
      this.years = [];
      for (let i = 0; i < 30; i++) {
        this.years.push({ value: year - i, label: year - i + "年" });
      }
    },
  }
};
function getFristChartClean(s){
  return s.substr(0,1);
}
</script>
